package com.higgins.eventtracking.client.health.secondary;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.HealthEventDTO;
import com.higgins.eventtracking.client.dto.HealthStatsDTO;
import com.higgins.eventtracking.client.dto.TrackingEventDTO;
import com.higgins.eventtracking.client.dto.WeightEventDTO;
import com.higgins.eventtracking.client.gui.components.AnnotatedChart;
import com.higgins.eventtracking.server.DateUtility;

public class BodyChart extends AnnotatedChart	{
	public void execute(String chartName, boolean weightEvent)	{
		setTitle(chartName);
		setColumnA("Date");
		setColumnB(chartName);
		setColumnC(null);
		setScale(150);
		if (weightEvent)  {
		    executeWeightEventChart();
		} else {
	        executeExerciseChart();
		}
	}
	
	   public void executeWeightEventChart()  {
	        if (columnC!=null)  {
	        } else {
	            greetingService.retrieveWeightEventChartDataDTO(columnB, new AsyncCallback<List<WeightEventDTO>>()    {

                    @Override
                    public void onFailure(Throwable caught) {
                        System.out.println(caught.toString());
                    }

                    @Override
                    public void onSuccess(List<WeightEventDTO> result) {
                        setWeightScale(result);
                        displayWeightChart(result);
                    }
	                
	            });
	        }
	    }
	
	public void executeExerciseChart()	{
		if (columnC!=null)	{
		} else {
			greetingService.retrieveExerciseChartDataDTO(columnB, new AsyncCallback<List<TrackingEventDTO>>()  {
				@Override
				public void onFailure(Throwable caught) {
				}
				@Override
				public void onSuccess(List<TrackingEventDTO> result) {
				    setScale(result);
					displayChart(result);
				}  });
		}
	}
    private void setWeightScale(List<WeightEventDTO> result)    {
        double maxWeight = 0.0;
        int scale = 150;
        for (WeightEventDTO dto:result)   {
            Double weight = dto.getWeightEventScale();
            if (weight>maxWeight)  {
                maxWeight = weight;
            }
        }
        if (maxWeight > 1000)  {
            scale = 1000;
        } else if (maxWeight <= 1000)   {
            scale = 1000;
        } else if (maxWeight <= 1000)   {
            scale = 1000;
        } else if (maxWeight <= 500)   {
            scale = 500;
        } else if (maxWeight <= 100)   {
            scale = 100;
        } else if (maxWeight <= 50)   {
            scale = 50;
        } else if (maxWeight <= 10)   {
            scale = 10;
        }
        setScale(150);
    }	
	private void setScale(List<TrackingEventDTO> result)    {
	    double maxWeight = 0.0;
	    int scale = 150;
	    for (TrackingEventDTO dto:result)   {
	        Double weight = dto.getWeight();
	        if (weight>maxWeight)  {
	            maxWeight = weight;
	        }
	    }
	    if (maxWeight > 1000)  {
	        scale = 1000;
	    } else if (maxWeight <= 1000)   {
	        scale = 1000;
	    } else if (maxWeight <= 1000)   {
            scale = 1000;
        } else if (maxWeight <= 500)   {
            scale = 500;
        } else if (maxWeight <= 100)   {
            scale = 100;
        } else if (maxWeight <= 50)   {
            scale = 50;
        } else if (maxWeight <= 10)   {
            scale = 10;
        }
	    setScale(150);
	}
	public void displayChart(final List<TrackingEventDTO> result)	{
        VisualizationUtils.loadVisualizationApi(new Runnable() {
            @Override
            public void run() {
                        JavaScriptObject jdygraph = createDygraph(
                                annotatedChart.getElement(), convertToDataTable(result), 0,
                                scale);
            }
        }, LineChart.PACKAGE);
	}

    public void displayWeightChart(final List<WeightEventDTO> result)   {
        VisualizationUtils.loadVisualizationApi(new Runnable() {
            @Override
            public void run() {
                        JavaScriptObject jdygraph = createDygraph(
                                annotatedChart.getElement(), convertToWeightDataTable(result), 0,
                                scale);
            }
        }, LineChart.PACKAGE);
    }	
	private DataTable convertToDataTable(List<TrackingEventDTO> results)	{
	    DataTable data = DataTable.create();
	    data.addColumn(ColumnType.DATE, columnA);
	    data.addColumn(ColumnType.NUMBER, columnB);
	    if (columnC!=null)	{
		    data.addColumn(ColumnType.NUMBER, columnC);
	    }
	    data.addRows(results.size());
	    int row = 0;
	    int col = 0;

	    for (TrackingEventDTO dto:results)	{
	    	data.setValue(row, col++, dto.getTimeStamp());

	    	if (columnB.equals("Walking") || columnB.equals("Running") || columnB.equals("Cycling") || columnB.equals("Swimming") )	{
		    	data.setValue(row, col++, dto.getDistance());
	    	} else {
		    	data.setValue(row, col++, dto.getWeight());
	    	}
	    	row++;
	    	col = 0;
	    }
		return data;
	}
    private DataTable convertToWeightDataTable(List<WeightEventDTO> results)    {
        DataTable data = DataTable.create();
        data.addColumn(ColumnType.DATE, columnA);
        data.addColumn(ColumnType.NUMBER, columnB);
        if (columnC!=null)  {
            data.addColumn(ColumnType.NUMBER, columnC);
        }
        data.addRows(results.size());
        int row = 0;
        int col = 0;

        for (WeightEventDTO dto:results)  {
            data.setValue(row, col++, dto.getTimestamp());

            data.setValue(row, col++, dto.getWeightEventScale());
            row++;
            col = 0;
        }
        return data;
    }	
}
